<?php
$version = Zend_Registry::get('version');
$global_conf = Zend_Registry::get('global_conf');
$ap_currency = new Zend_Currency($global_conf['default_locale']);
?>

<script type="text/javascript">
$(document).ready(function() {	
	floatingbar('#floatingbar',"#button_top_bar",null);
	function openMsgDialog(msg)
	{
		$('#dialog_msg').html(msg).dialog({
			autoOpen: true,
			modal: true,
			show: 'fade',
			resizable: 'false',				
			buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
			open: function(event, ui)
				 {
					setTimeout(function() 
					{									
						if($('#' + event.target.id).dialog('isOpen'))
						{
							$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
						}
					}, 10000);
				  }
		});
	}
	
	$("#upload-form").dialog({
		autoOpen: false,
		width: 860,
		height: 600,
		show: 'fade',			
		modal: true
	});	
	
	$('a.upload_btn').click(function() {
		var self = this;
			
		$("#i-upload-form").attr("src", "<?php echo $this->url( array('module' => 'Paymentgateway','controller' => 'backendgateway', 'action'     => 'uploadfile' ), 'adminrout',    true) ?>");			
		$('#upload-form').dialog('open');
	});	
	
	$('a.select_file_btn').click(function() {		 
			var currentIFrame = $('#i-upload-form');
			var v = currentIFrame.contents().find("#file_name").val();
			if(v == '' || v == null)
			{
				var msg = "<?php echo $this->translator->translator('insert_selected_file_err'); ?>";				
				openMsgDialog(msg);
			}
			else
			{
				var file_arr =  v.split(',');							
				var img_code = '<span class="thumb"><img src="'+file_arr[0]+'/'+file_arr[1]+'" width="45" height="35" /></span>';
				$('#selected_file').html(img_code);
				$('#logo').val(file_arr[1]);
				$("#upload-form").dialog('option','hide','explode').dialog('close');
				imageThumbnil();
			}
		});
	
	$('a.close_dialog_btn').click(function() {		 
		$("#upload-form").dialog('option','hide','explode').dialog('close');			
	});
	
	$('a.reset_btn').click(function() {			
		 resetFields();	
		 removeError();	
	});
	
	function resetFields()
	{
		$( "form" )[ 0 ].reset();
		$('span#selected_file').html('');
	}
	
	function removeError()
	{
		$('#actionMessage').html('');
		
		var filename = '';
		var filevalue = '';		
		$.each($('form').serializeArray(), function(i, field) {	
				if(field.name.match(/\[\]$/))
				{
					var filearr = field.name.split('[]');
					filename = filearr[0];					
				}
				else
				{
					filename 	= 	field.name;
				} 			
				$('#'+filename).removeClass('ui-state-error');
				$('#'+filename+'_err').html('');									
		});				
	}
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');
		$('#'+err_id).append(errMsg);
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	function getFormData()
	{
		var fieldObj = {};
		var filename = '';
		var filevalue = '';
		var poll_answer	= new Array(1);
		$.each($('form').serializeArray(), function(i, field) {	
			if(field.name.match(/\[\]$/))
			{
				/*$("input[name='poll_answer[]']").each(function(id,field) { 				
					poll_answer[id] 		= field.value;
				});
				var filename	= 	field.name.split('[]');
				fieldObj[filename[0]] = poll_answer;
				count++;*/
			}
			else
			{
				filename 	= 	field.name;
				filevalue 	= 	field.value;
				fieldObj[filename] = filevalue;			
			}	
		});
		return fieldObj;
	}
	
	$('a.add_gateway_btn').click(function() {			
			
		var formData = getFormData();	
						
		$.ajax({
				url: "<?php echo $this->url( array('module' => 'Paymentgateway','controller' => 'backendgateway', 'action'     => 'add' ), 'adminrout',    true) ?>",
			  	type: 'POST',
				beforeSend: function(){						
						removeError();
						openLoaderDialog();				
					 },
			  	data: formData,
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");					
					closeLoaderDialog();					
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));			
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
				},
				error: function(xhr, status, error){
					closeLoaderDialog();
					var msg = "Error! " + xhr.status + " " + error;
					$('#actionMessage').html(errMsgDesign(msg));
				}
		});
	});
	
	/************************************************IMAGE THUMBNIL************************************************/
	function imageThumbnil()
	{	
		$("span.thumb").hover(function() {
			$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
			$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
				.animate({
					marginTop: '-110px', /* The next 4 lines will vertically align this image */ 
					marginLeft: '-110px',
					top: '50%',
					left: '50%',
					width: '174px', /* Set new width */
					height: '174px', /* Set new height */
					padding: '20px'
				}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
		
			} , function() {
			$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
			$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
				.animate({
					marginTop: '0', /* Set alignment back to default */
					marginLeft: '0',
					top: '0',
	
					left: '0',
					width: '30px', /* Set width back to default */
					height: '20px', /* Set height back to default */
					padding: '5px'
				}, 400);
		});
	}
});
</script>

<div id="container">
<?php if (!$this->allow('paypal')) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>
<div id="button_top_bar"></div>
<div class="title-container">
	<div class="gateways-edit"><h1><?php echo $this->translator->translator('gateway_list_page_new'); ?></h1></div>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Paymentgateway','controller' => 'backendgateway', 'action'     => 'list'), 'adminrout',    true) ?>" class="back_btn_link"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="add_gateway_btn"><span class="icon save"><?php echo $this->translator->translator('gateway_add_page_save'); ?></span></a></span>
    </div>
</div>

<div class="content-container">
	<div id="actionMessage"></div>
	<form name="formGateway" id="formGateway" method="post">    	
        <div id="generalInfo">
        	<fieldset class="fieldset last">
                <legend class="legend"><?php echo $this->translator->translator('gateway_add_page_info'); ?></legend>
                <div class="form">
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->gwname->getLabel();  if($this->gatewayForm->gwname->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->gwname; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->gwname->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="gwname_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->name->getLabel();  if($this->gatewayForm->name->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->name; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->name->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="name_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->ap_merchant->getLabel();  if($this->gatewayForm->ap_merchant->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->ap_merchant; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->ap_merchant->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="ap_merchant_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->security_code->getLabel();  if($this->gatewayForm->security_code->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->security_code; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->security_code->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="security_code_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->api_password->getLabel();  if($this->gatewayForm->api_password->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->api_password; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->api_password->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="api_password_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->ap_currency->getLabel();  if($this->gatewayForm->ap_currency->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->ap_currency; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->ap_currency->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="ap_currency_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->ap_returnurl->getLabel();  if($this->gatewayForm->ap_returnurl->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->ap_returnurl; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->ap_returnurl->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="ap_currency_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->ap_cancelurl->getLabel();  if($this->gatewayForm->ap_cancelurl->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->ap_cancelurl; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->ap_cancelurl->getAttrib('title');?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="ap_currency_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->test_mode->getLabel(); if($this->gatewayForm->test_mode->isRequired()){ ?><span class="required">*</span><?php } ?>
                        </div>
                        
                        <div class="field">
                            <?php echo $this->gatewayForm->test_mode; ?>
                            <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->gatewayForm->test_mode->getAttrib('title') ;?> " ><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                            <div class="input-errors" id="test_mode_err"></div>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="label">
                            <?php echo $this->gatewayForm->gateway_id; ?>
                        </div>
                        
                        <div class="field">
                            
                        </div>
                    </div>
             	</div>
         	</fieldset>
     	</div>
  	</form>
</div>

<div id="floatingbar">
	<div class="gateways-edit"><h1><?php echo $this->translator->translator('gateway_list_page_new'); ?></h1></div>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Paymentgateway','controller' => 'backendgateway', 'action'     => 'list'), 'adminrout',    true) ?>" class="back_btn_link"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="add_gateway_btn"><span class="icon save"><?php echo $this->translator->translator('gateway_add_page_save'); ?></span></a></span>
    </div>
</div>
<?php endif; ?>

<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg', $this->translator->translator('gateway_list_page_new')); ?>">
</div>
<div id="dialog_container" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar" ></div>	
</div>
<div id="upload-form" title="<?php echo $this->translator->translator('gateway_page_dialog_upload'); ?>">
	<div>
    	<iframe id="i-upload-form" src="" class="iframe-img-upload"></iframe>
        
        <div class="button-container">
            <span class="button ui-widget-content-1"><a href="javascript:void(0);" class="select_file_btn"><span class="icon insert"><?php echo $this->translator->translator('gateway_page_insert_upload'); ?></span></a></span>
            <span class="button ui-widget-content-1"><a href="javascript:void(0);" class="close_dialog_btn"><span class="icon close"><?php echo $this->translator->translator('common_close'); ?></span></a></span>
        </div>
    </div>
</div>
</div>